1 ========================================================================
2 CONSOLE APPLICATION : VBFileMappingServer Project Overview
3 ========================================================================
5 /////////////////////////////////////////////////////////////////////////////
8 File mapping is a mechanism for one-way or duplex inter-process communication
9 among two or more processes in the local machine. To share a file or memory,
10 all of the processes must use the name or the handle of the same file mapping
13 To share a file, the first process creates or opens a file by using the
14 CreateFile function. Next, it creates a file mapping object by using the
15 CreateFileMapping function, specifying the file handle and a name for the
16 file mapping object. The names of event, semaphore, mutex, waitable timer,
17 job, and file mapping objects share the same name space. Therefore, the
18 CreateFileMapping and OpenFileMapping functions fail if they specify a name
19 that is in use by an object of another type.
21 To share memory that is not associated with a file, a process must use the
22 CreateFileMapping function and specify INVALID_HANDLE_VALUE as the hFile
23 parameter instead of an existing file handle. The corresponding file mapping
24 object accesses memory backed by the system paging file. You must specify
25 a size greater than zero when you use an hFile of INVALID_HANDLE_VALUE in a
26 call to CreateFileMapping.
28 Processes that share files or memory must create file views by using the
29 MapViewOfFile or MapViewOfFileEx function. They must coordinate their access
30 using semaphores, mutexes, events, or some other mutual exclusion technique.
32 The VB.NET code sample demonstrates creating a file mapping object named
33 "Local\SampleMap" and writing a string to the file mapping. Because the Base
34 Class Library of .NET Framework 2/3/3.5 does not have any public classes to
35 operate on file mapping objects, you have to P/Invoke the Windows APIs as
36 shown in this code sample.
39 /////////////////////////////////////////////////////////////////////////////
42 The following steps walk through a demonstration of the file mapping sample.
44 Step1. After you successfully build the VBFileMappingClient and
45 VBFileMappingServer sample projects in Visual Studio 2008, you will get the
46 applications: VBFileMappingClient.exe and VBFileMappingServer.exe.
48 Step2. Run VBFileMappingServer.exe in a command prompt. The application will
49 create a file mapping object of a specified size that is backed by the system
50 paging file. Its name is "Local\SampleMap".
52 The file mapping (Local\SampleMap) is created
54 Next, the application maps a view of the file mapping into the address space
55 of the process, and writes a string to the view.
57 The file view is mapped
58 This message is written to the view:
59 "Message from the first process."
61 Step3. Run VBFileMappingClient.exe in another command prompt.
62 VBFileMappingClient opens the file mapping object "Local\SampleMap", maps
63 the same view of the file mapping into its address space, and read the string
64 written by the first process from the view.
66 The file mapping (Local\SampleMap) is opened
67 The file view is mapped
68 Read from the file mapping:
69 "Message from the first process."
71 Step4. Press ENTER in both command prompts to close VBFileMappingServer and
75 /////////////////////////////////////////////////////////////////////////////
77 (The relationship between the current sample and the rest samples in
78 Microsoft All-In-One Code Framework http://1code.codeplex.com)
80 VBFileMappingClient -> VBFileMappingServer
81 VBFileMappingServer creates the file mapping named "Local\SampleMap" and
82 writes a string to it. VBFileMappingClient reads the string from the file
86 /////////////////////////////////////////////////////////////////////////////
89 1. Create a file mapping named "Local\SampleMap" by P/Invoking
92 2. Map a view of the file mapping into the address space of the current
93 process by P/Invoking MapViewOfFile.
95 3. Write a string to the file view.
97 4. Unmap the file view (UnmapViewOfFile) and close the file mapping object
101 /////////////////////////////////////////////////////////////////////////////
104 MSDN: Creating Named Shared Memory
105 http://msdn.microsoft.com/en-us/library/aa366551.aspx
108 /////////////////////////////////////////////////////////////////////////////